/*
* ============================================================================
*  Name     : GV60SettingsView.cpp
*  Part of  : GV60
*  Copyright: Wipo Software
* ============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknviewappui.h>
#include <aknnotewrappers.h>
#include <barsread.h>

#include <GV60.rsg>
#include "GV60.hrh"
#include "GV60AppUi.h"
#include "GV60SettingsView.h"
#include "GV60SettingsSettingList.h"
#include "GV60Config.h"


// ================= MEMBER FUNCTIONS =======================

void CGV60SettingsView::ConstructL()
{
  BaseConstructL(R_GV60_SETTINGS_VIEW);
}

CGV60SettingsView::~CGV60SettingsView()
{
  if (m_pSettingList)
  {
    AppUi()->RemoveFromViewStack(*this, m_pSettingList);
    delete m_pSettingList;
    m_pSettingList = NULL;
  }
}

TUid CGV60SettingsView::Id() const
{
  return KSettingsViewId;
}

void CGV60SettingsView::SizeChanged()
{
  if (m_pSettingList)
  {
    m_pSettingList->SetRect(ClientRect());
  }
}

void CGV60SettingsView::HandleCommandL(TInt aCommand)
{   
  switch ( aCommand )
  {
    case EAknCmdExit:
    case EAknSoftkeyBack:
      RetrieveAndSaveEventL();
      AppUi()->ActivateLocalViewL(m_iPreviousView);
      break;

    case EGV60CmdChangeValue:
      if (m_pSettingList)
      {
        m_pSettingList->EditCurrentItemL();
      }
      break;

    default:
      AppUi()->HandleCommandL( aCommand );
      break;
  }
}

void CGV60SettingsView::HandleClientRectChange()
{
  if (m_pSettingList)
  {
    m_pSettingList->SetRect(ClientRect());
  }
}

void CGV60SettingsView::DoActivateL(const TVwsViewId& aPrevViewId, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
{
  m_iPreviousView = aPrevViewId.iViewUid;

  CGV60AppUi* pAppUi = reinterpret_cast<CGV60AppUi*>(AppUi());
  User::LeaveIfNull(pAppUi);

  CGV60Configuration& iConfiguration = pAppUi->GetConfig();
  
  m_pSettingList = CGV60SettingsSettingList::NewL(iConfiguration);
  m_pSettingList->SetMopParent(this);

  // Allocate a buffer to be used by the TResourceReader and pushes onto the cleanup stack
  TResourceReader reader;
  CEikonEnv::Static()->CreateResourceReaderLC(reader, R_GV60_SETTINGS_ITEMLIST);
  m_pSettingList->ConstructFromResourceL(reader);

  // Clean up the buffer allocated above, NOT the reader itself.
  CleanupStack::PopAndDestroy();  

  AppUi()->AddToViewStackL(*this, m_pSettingList);
  m_pSettingList->ActivateL();
}

void CGV60SettingsView::DoDeactivate()
{
  if (m_pSettingList)
  {
    AppUi()->RemoveFromViewStack(*this, m_pSettingList);
    delete m_pSettingList;
    m_pSettingList = NULL;
  }
}

void CGV60SettingsView::RetrieveAndSaveEventL()
{
  CGV60AppUi* pAppUi = reinterpret_cast<CGV60AppUi*>(AppUi());
  
  if (m_pSettingList && pAppUi)
  {
    CGV60Configuration& iConfiguration = pAppUi->GetConfig();

    m_pSettingList->StoreSettings();
    if (m_pSettingList->GetValues(iConfiguration))
    {
      iConfiguration.WriteToFile();
    }
  }
}


